package com.wn.inventory.web;

import com.wn.common.PageBean;
import com.wn.common.ResponseEntity;
import com.wn.entity.InventoryBookkeeping;
import com.wn.inventory.service.InventoryBookkeepingService;
import com.wn.inventory.vo.InventoryBookkeepingVo;
import com.wn.utils.PageBeanUtil;
import com.wn.validation_group.BookkeepingAddGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @ author     ：NST
 * @ date       ：Created in 2022/10/19 16:16
 * @ description：
 * @ modified By：
 * @ version:
 */
@RequestMapping("inventory/bookkeeping")
@RestController
public class InventoryBookkeepingController {
    @Autowired
    InventoryBookkeepingService inventoryBookkeepingService;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    /**
     * @param inventoryBookkeeping
     * @return com.wn.common.ResponseEntity
     * @ author nst
     * @ date 2022/10/24 16:08
     * @ Description 前端需传入物料档案id,批次号或者工单号,库存,库位,采购编号
     */
    @RequestMapping("add")
    public ResponseEntity add(@RequestBody @Validated({BookkeepingAddGroup.class}) InventoryBookkeeping inventoryBookkeeping) {
        inventoryBookkeepingService.insert(inventoryBookkeeping);
        return ResponseEntity.success;
    }

    @RequestMapping("addition")
    public ResponseEntity addition(@RequestBody InventoryBookkeepingVo inventoryBookkeepingVo) {
        HashOperations hash = redisTemplate.opsForHash();
        //优先查询redis缓存中是否有信息，有直接返回redis中的缓存信息
        if (hash.hasKey("inventory", inventoryBookkeepingVo.toString())) {
            PageBean bean = (PageBean) hash.get("inventory", inventoryBookkeepingVo.toString());
            return new ResponseEntity(bean);
        }
        PageBean<InventoryBookkeeping> pageBean = inventoryBookkeepingService.selectByCondition(inventoryBookkeepingVo);
        //把此次查询内容放入redis 过期时间设置为5分钟
        hash.put("inventory",inventoryBookkeepingVo.toString(),pageBean);
        redisTemplate.expire("inventory",5, TimeUnit.MINUTES);

        return new ResponseEntity(pageBean);
    }

    @RequestMapping("update")
    public ResponseEntity update(@RequestBody  InventoryBookkeeping inventoryBookkeeping) {
        if (inventoryBookkeeping.getInventory() == 0 && inventoryBookkeeping.getWarehouseSite() == null && inventoryBookkeeping.getState() == null) {
            return new ResponseEntity("提供正确修改数据！");
        }
        inventoryBookkeepingService.update(inventoryBookkeeping);
        return ResponseEntity.success;
    }
}
